<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta name="author" content="wangyuguo">
		<title>手续费设置</title>
		<script type="text/javascript" src="../script/common.js"></script>
		
		<script type="text/javascript">
			//全局变量
			var qt = null;//数据表格对象
			
			function load() {
				Eui.showNavigation();
				setPages(["listview","editview"]);
		
				showRules()
			}
			
			function showRules(){
				DWR.call("ruleMgntService.getRulesForOrg",RuleCategory.DISCOUNT_PURCHASE,function(result){
					var html = ""
					if (result){
						var orgRules
						var rule
						for(var i=0;i<result.length;i++){
							orgRules = result[i]
							html += orgRules.orgName+orgRules.orgUniqueId
							html +="<table>"
							html +="<tr>"
							html +="<th>规则描述</th>"
							html +="<th>启用时间</th>"
							html +="<th>失效时间</th>"
							html +="</tr>"
							for (var j=0;j<orgRules.rules.length;j++){
								rule = orgRules.rules[j]
								
								html += "<tr><td>"
								html += rule.judgeDesc
								html += "</td><td>"
								html += rule.beginTime?rule.beginTime:"批准后启用"
								html += "</td><td>"
								html += rule.endTime?rule.endTime:"一直有效"
								html += "</td></tr>"
							}
							html +="</table>"
						}
					
					}
					$("discounttable").innerHTML = html
				})
			}
			
			var ruleList		//当前编辑的
			var ruleConds
			function onEdit(){
				switchPage('editview');
				DWR.call("ruleMgntService.editRulesOfOrg",RuleCategory.DISCOUNT_PURCHASE,function(orgRules){
					var html = ""
					if (orgRules){
						html += orgRules.orgName+orgRules.orgUniqueId
						html +="<table>"
						html +="<tr>"
						html +="<th>规则描述</th>"
						html +="<th>启用时间</th>"
						html +="<th>失效时间</th>"
						html +="<th>操作</th>"
						html +="</tr>"
						ruleList = orgRules.rules
						
						var rule
						for (var j=0;j<ruleList.length;j++){
							rule = ruleList[j]
							
							html += "<tr><td><a href=# onclick='editRule("+j+")'>"
							html += rule.judgeDesc
							html += "</a></td><td>"
							html += rule.beginTime?rule.beginTime:"批准后启用"
							html += "</td><td>"
							html += rule.endTime?rule.endTime:"一直有效"
							html += "</td><td><a href=# onclick='deleteRule("+j+")'>删除</a></span>"
							html += "</td></tr>"
						}
						html +="</table>"
					
					}
					$("editlist").innerHTML = html
				})
				
				ruleConds = DWR.syncCall("ruleMgntService.getRuleConds",RuleCategory.DISCOUNT_PURCHASE)
				Common.initDict(ruleForm.ruleConds,ruleConds,null,"name","name")
				
				changeCond()
				
			}
			
			function changeCond(){
				var index = ruleForm.ruleConds.selectedIndex
				if (index==-1) return;
				
				var ruleFuncs = DWR.syncCall("ruleMgntService.getRuleFuncs",ruleConds[index].dataType)
				
				Common.initDict(ruleForm.ruleFuncs,ruleFuncs,null,"name","text")
				
				changeFunc()
			}
		
			function changeFunc(){
				var condIndex = ruleForm.ruleConds.selectedIndex
				if (condIndex==-1) return;
				
				var funcIndex = ruleForm.ruleFuncs.selectedIndex
				if (funcIndex==-1) return;
				
				$('ruleValueDiv').innerHTML = getEditControl(ruleConds[condIndex])
			}
			
			
			function getEditControl(cond){
				var html = ""
				if (cond.detailType=="ContractType"){
					html = "<select name=valueSelect>"
					var cts = DWR.syncCall("mgntQueryService.findContractTypes")
					for(var i = 0 ; i< cts.length; i++){
						html += "<option value='"
						html += cts[i].text
						html += "'>"
						html += cts[i].text
						html += "</option>"
					}
					html += "</select>"
				}else if (cond.detailType=="GoodsCategory"){
					html = Common.getMultiSelectHtml("商品类别","ruleForm","valueSelect","/card/sel_goods_category.html",460,15);
				}else if (cond.detailType=="Gift"){
					html = "<select name=valueSelect><option>1234</option></select>"
				}
				else
					html = "<input type=textfield name=valueSelect>"
			
				return html
			}
			
			function editRule(id){
				var html =""
				var rule = ruleList[id]
				if (rule.unitExprs){
					var unitExpr,unitDesc
					html += "<ul>"
					for (var j=0;j<rule.unitExprs.length;j++){
						unitExpr = rule.unitExprs[j]
						unitDesc = rule.unitDescs[j]
						
						html += "<li>"
						html += unitDesc
						
						html += "<span class=deleteruleSpan><a href=# onclick='deleteExpr()'>删除</a></span>"
						html += "</li>"
					}
					html += "</ul>"
				}
				
						
				$('ruleExprList').innerHTML = html
			}
			
			function onCancel(){
				onRefresh()
			}
			
			function onRefresh(){
				switchPage("listview");
				showRules()
			}
			
			//删除一条规则
			function deleteRule(){
			}
			
			//增加一条规则
			function addRule(){
			}
			
			//删除一条规则表达式
			function deleteExpr(){
			}
			
			//增加一条规则表达式
			function addExpr(){
				$('newrulediv').style.display = ""
			}
			
			function onSave(){
				if (!checkForm(feeForm)) return;
				var fee = Common.getValues(feeForm);
				fee.amount = Format.yuanToFen(fee.amount);
				
				DWR.call("ruleMgntService.saveRules",fee,function(v){
					showInformation("手续费保存成功！");
					feeForm.reset();
					switchPage("listview");
					qt.showTable();
				},{blockScreen:true});
			}
			
		</script>
		<style>
			.logicAnd{
				color:red;
				font-weight:bold
			}
			
			.deleteruleSpan{
				padding-left:20px
			}
		</style>
	</head>
	<body onload="load()">
		<!--页面导航-->
		<div class="nav">
			<div id="nav">&nbsp;</div>
		</div>
		<div id="listview">
			<div class="form">
				<div class="formTitle">
					<table cellSpacing="0" cellPadding="0" width="100%" border="0">
						<tr>
							<td>当前规则</td>
							<td align="right">
								<button onclick="onEdit()">编辑规则</button>
							</td>
						</tr>
					</table>
				</div>
				<div id="discounttable"></div>
			</div>
		</div>
		
		<div id="editview" style="display:none">
			<div class="form">
				<div class="formTitle">
					<table cellSpacing="0" cellPadding="0" width="100%" border="0">
						<tr>
							<td>规则列表</td>
							<td align="right">
								<button onclick="addRule()">增加规则</button>
								<button onclick="onSave()">保存</button>
								<button onclick="onCancel()">取消编辑</button>
							</td>
						</tr>
					</table>
				</div>
				<div id="editlist"></div>
			</div>
			
			<form id="ruleForm" name="ruleForm">
				<div class="form">
			    <div class="formTitle">规则详细信息</div>
			    <table cellSpacing="0" cellPadding="4" width="100%" border="0" class="label5">
			       <tr>
			       		 <td><label>启用日期</label> <input type="text" name="beginTime" maxlength="20"
		                                        onblur="checkV(this,'futureDate');"/><a id="ruleForm_beginTime" href="javascript:void(0)" onclick="showDatePicker(ruleForm.beginTime);" class="date" title="选择日期">&nbsp;&nbsp;</a></td>
		                
					     <td><label>失效日期</label> <input type="text" name="endTime" maxlength="20"
		                                        onblur="checkV(this,'futureDate');"/><a id="ruleForm_endTime" href="javascript:void(0)" onclick="showDatePicker(ruleForm.endTime);" class="date" title="选择日期">&nbsp;&nbsp;</a></td>
		           </tr>
			    </table>
			    以下条件需同时满足：
			    <div id="ruleExprList">
				</div>
				<a href="#" onclick="addExpr()">增加条件</a>
				<div id="newrulediv" style="display:none">
					<select name="ruleConds" onchange="changeCond()"><option></option></select>
					<select name="ruleFuncs"><option></option></select>
					<div id="ruleValueDiv"></div>
					<a href="#" onclick="addExpr()">增加条件</a>
				</div>
			</form>		
					
		</div>
		
		
	</body>
</html>